<template>
  <div class="app-container">
    <!-- 添加或修改项目基础信息管理对话框 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="生产单号" prop="produceNo">
        <el-input v-model="queryParams.produceNo" placeholder="请输入生产单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单编号" prop="projectNo">
        <el-input v-model="queryParams.projectNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品编码" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入产品编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工程名称" prop="gongchengName">
        <el-input v-model="queryParams.gongchengName" placeholder="请输入工程名称" clearable
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="需求日期" prop="saleDate">
        <el-date-picker clearable v-model="queryParams.saleDate" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择需求日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="产品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入产品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="产品型号" prop="xinghao">
        <el-input v-model="queryParams.xinghao" placeholder="请输入产品型号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <!--<el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExportDetail" >导出
        </el-button>-->
      </el-form-item>
    </el-form>
    <!--<el-table :data="dataList" :max-height="tableMaxHeight" @selection-change="handleSelectionChange"
              v-loading="loading">-->
    <el-col :span="1.5">
      <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="multiple" @click="piliangFenpeiGx"
        v-hasPermi="['develop:FenpeiGxList:piliangProcessBnt']">批量分配工艺工序</el-button>
    </el-col>
    <el-table border :data="columns" ref="dragTable" row-key="id" v-loading="loading" @selection-change="handleSelectionChange" show-summary :summary-method="getSummaries"
      @sort-change="handleSortChange" :max-height="tableMaxHeight">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="序号" type="index" width="50">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column sortable label="生产单号" align="center" prop="produceNo" />
      <el-table-column sortable label="订单编号" align="center" prop="projectNo" show-overflow-tooltip >
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="right">
            <el-tag size="medium" style="cursor: pointer" @click="chsoeCopy(scope.row.projectNo)">点击复制内容</el-tag>
            <div slot="reference" class="name-wrapper">
              <span>{{ scope.row.projectNo }}</span>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column sortable label="客户名称" align="center" prop="customName" show-overflow-tooltip />
      <el-table-column sortable label="工程名称" align="center" prop="gongchengName" show-overflow-tooltip />
      <!-- <el-table-column label="产品编码" align="center" prop="goodsCode"/> -->
      <el-table-column sortable label="产品编码" align="center" prop="code">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="right">
            <el-tag size="medium" style="cursor: pointer" @click="chsoeCopy(scope.row.code)">点击复制内容</el-tag>
            <div slot="reference" class="name-wrapper">
              <span>{{ scope.row.code }}</span>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column sortable label="产品状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!--<el-table-column sortable align="center" label="工序分配状态" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sale_state" :value="scope.row.state" />
        </template>
      </el-table-column>-->
      <el-table-column sortable align="center" label="产品名称" prop="name" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="right">
            <el-tag size="medium" style="cursor: pointer" @click="chsoeCopy(scope.row.name)">点击复制内容</el-tag>
            <div slot="reference" class="name-wrapper">
              <span>{{ scope.row.name }}</span>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column sortable align="center" label="产品型号" prop="xinghao">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="right">
            <el-tag size="medium" style="cursor: pointer" @click="chsoeCopy(scope.row.xinghao)">点击复制内容</el-tag>
            <div slot="reference" class="name-wrapper">
              <span>{{ scope.row.xinghao }}</span>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column sortable align="center" label="自制数量" prop="count" />
      <el-table-column label="下单日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="需求日期" align="center" prop="saleDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.saleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="产品描述" prop="miaoshu" show-overflow-tooltip />
      <el-table-column align="center" label="备注" prop="remark" show-overflow-tooltip />
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button @click="choseGy(scope.row)" icon="el-icon-view"
            size="mini" type="text" v-hasPermi="['develop:FenpeiGxList:settingProcessBnt']">
            设定工序1
          </el-button>
          <el-button @click="lookGy(scope.row)"  icon="el-icon-view"
            size="mini" type="text" v-hasPermi="['develop:FenpeiGxList:LookProcessBnt']">
            工序查看
          </el-button>
         <!-- <el-button @click="chaijie(scope.row)" icon="el-icon-view" size="mini" type="text"
            v-hasPermi="['develop:FenpeiGxList:chaijieBnt']">
            串行拆解
          </el-button>-->
          <!--<el-button @click="xunjian(scope.row)" icon="el-icon-view" size="mini" type="text"
            v-hasPermi="['develop:FenpeiGxList:addPollingBnt']">
            巡检单
          </el-button>-->
          <el-button @click="toOther(scope.row)" icon="el-icon-view" size="mini" type="text">
            产品不自制
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <choseGx-form @ok="getList" ref="choseGxForm"></choseGx-form>
    <piliangFenpeiGxp-form @ok="getList" ref="piliangFenpeiGxpForm"></piliangFenpeiGxp-form>
    <chajie-form @ok="getList" ref="chaijieForm"></chajie-form>
    <fpTaskForm-list @ok="getList" ref="taskFpForm"></fpTaskForm-list>
    <xunjian-form @ok="getList" ref="xunjianForm"></xunjian-form>
    <goodsSaleGongxu-form @ok="getList" ref="goodsSaleGongxuForm"></goodsSaleGongxu-form>
    <toOther-form @ok="getList" ref="toOtherform"></toOther-form>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import {
  addAction,
  deleteAction,
  downloadFile,
  getAction,
  getById,
  chsoeCopy,
  accAdd
} from '@/api/manage'
import ChoseGxForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ChoseGxForm";
import PiliangFenpeiGxpForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/PiliangFenpeiGxpForm";
import ChajieForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ChajieForm";
import FpTaskFormList from "@/views/develop/projectBaseInfo/prjForGoodsIn/FpTaskFormList";
import XunjianForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/XunjianForm";
import GoodsSaleGongxuForm from "@/views/develop/goodsSaledoGongxu/module/GoodsSaleGongxuForm";
import ToOtherForm from "@/views/develop/projectBaseInfo/prjForGoodsIn/ToOtherForm";

export default {
  name: "FenpeiGxList",
  dicts: ['isdo', 'sale_state', 'goods_status'],
  components: {
    ChoseGxForm,
    PiliangFenpeiGxpForm,
    ChajieForm,
    FpTaskFormList,
    XunjianForm,
    GoodsSaleGongxuForm,
    ToOtherForm,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "设定产品工序",
      // 是否显示弹出层
      open: false,
      single: true,
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 表单参数
      form: {},
      proId: "",
      proStatus: "",
      columns: [],
      total: 0,
      ids: [],
      goodsIds: [],
      // 表单校验
      rules: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        params: {},
        id: null,
        code: null,
        projectNo: null,
        produceNo: null,
        name: null,
        goodsCode: null,
        model: null,
        xinghao: null,
        gongchengName: null,
        //status:'1';
        issheding: "0",//未分配工艺工序
        remark: null,
        projectId: null,
        orderByColumn: "create_time",
        isAsc: "desc"
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 10000,
        params: {},
        id: null,
      },
      url: {
        list: '/factoryProduceSaleAll/factoryProduceSaleAll/list',
        getById: '/projectBaseInfo/projectBaseInfo/',
        exportData: '/goodsSale/goodsSale/exportData',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    //this.handleQuery();
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  methods: {
    toOther(row){
      this.$refs.toOtherform.handleUpdate(row)
    },
    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if ( column.property === "count") {
          sums[index] = values.reduce((prev, curr) => {

            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev , curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },    chsoeCopy(scope) {
      const save = function (e) {
        e.clipboardData.setData("text/plain", scope);
        e.preventDefault();                     // 阻止默认行为
      };
      document.addEventListener("copy", save);  // 添加一个copy事件
      document.execCommand("copy");             // 执行copy方法
      // 复制成功提示
      this.$message({ message: "复制成功", type: "success" });
    },
    lookGy(row) {
      this.$refs.goodsSaleGongxuForm.xunjianShow(row);
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    xunjian(row) {
      this.$refs.xunjianForm.xunjianShow(row);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.goodsIds = selection.map(item => item.goodsId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    piliangFenpeiGx() {
      // 检查 this.goodsIds 中是否都一样，不一样进行提示
      if (this.goodsIds.length > 1) {
        const firstGoodsId = this.goodsIds[0];
        const allSame = this.goodsIds.every(goodsId => goodsId === firstGoodsId);
        if (!allSame) {
          this.$message.warning('选择的产品编码不一致，请重新选择');
        } else {
          this.$refs.piliangFenpeiGxpForm.piliangFenpeiGxpShow(this.ids, this.goodsIds);
        }
      } else {
        this.$refs.piliangFenpeiGxpForm.piliangFenpeiGxpShow(this.ids, this.goodsIds);
      }

    },
    taskFp(row) {
      this.$refs.taskFpForm.taskFpShow(row);
    },
    chaijie(row) {
      this.$refs.chaijieForm.chaijieGoods(row);
    },
    choseGy(row) {
      this.$refs.choseGxForm.choseGy(row);
    },

    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.columns = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params.salesmanName = ''
      this.queryParams.params.orderDate = ''
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },

    handleExportDetail(row) {
      downloadFile(this.url.exportData, this.queryParams);
    },


  }
};
</script>
